package drongo.desktop.menu
{
	import drongo.desktop.undo.UndoSystem;
	import drongo.desktop.utils.tr;
	
	import flash.display.NativeMenu;

	public class MenuHelper
	{
		public function MenuHelper()
		{
			
		}
		/**
		 * 通过XML来创建菜单
		 * @param xml
		 * @return
		 */		
		public static function createByXML(xml:XML,menu:NativeMenu=null,undo:UndoSystem=null):NativeMenu{
			var root:NativeMenu;
			if(menu==null){
				root=new NativeMenu();
			}else{
				root=menu;
			}
			_createByXML(xml,root,undo);
			return root;
		}
		private static function _createByXML(xml:XML,root:NativeMenu=null,undo:UndoSystem=null):void{
			var xmlList:XMLList=xml.children();
			var subXML:XML;
			var subXMLList:XMLList;
			var subMenu:Object;
			//菜单属性
			var isSeparator:Boolean;
			var toggled:Boolean;
			var index:uint=0;
			
			var menus:Array=[];
			var item:DGNativeMenuItem;
			var menu:NativeMenu;
			
			var label:String;
			for each (subXML in xmlList) 
			{
				subXMLList=subXML.children();
				if(subXML.@isSeparator!=null&&subXML.@isSeparator!=""){
					isSeparator=subXML.@isSeparator=="true"?true:false;
				}
				if(subXML.@type!=null&&subXML.@type!=""){
					toggled=subXML.@toggled=="true"?true:false;
				}
				if(undo){
					label=tr(subXML.@label)+undo.getShotKeyToString(subXML.@value);
				}else{
					label=tr(subXML.@label);
				}
				item=new DGNativeMenuItem(label,isSeparator);
				item.toggled=toggled;
				item.data=subXML;
				menus[index]=item;
				if(subXMLList.length()>0){
					menu=new NativeMenu();
					_createByXML(subXML,menu,undo);
					item.submenu=menu;
				}
				
				index++;
			}
			root.items=menus;
		}
	}
}